package industry;

import java.util.Random;
import main.Plugin;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.inventory.ItemStack;

import actives.ActiveManager;
import actives.ActiveType;
import actives.Active.ActiveState;

import skills.Skill;
import skills.Skills;
import skills.SkillHandler;

public class Woodcutting implements Listener { 

	public enum Wood {
		OAK, SPRUCE, BIRCH, JUNGLE;
	}

	public Woodcutting(Plugin plugin) {
		Bukkit.getPluginManager().registerEvents(this, plugin);
	}

	@EventHandler
	private void blockDamage(BlockDamageEvent event) {
		Player player = event.getPlayer();
		boolean axe = player.getItemInHand().getType().name().contains("_AXE");
		if (axe) {
			Block block = event.getBlock();
			Skill woodcutting = SkillHandler.getSkill(player, Skills.WOODCUTTING);
			if (block.getType().equals(Material.LEAVES)) {
				if (woodcutting.level() >= 250) {
					block.breakNaturally();
				}
			}
		}
	}

	@EventHandler
	private void blockBreak(BlockBreakEvent event) {
		Player player = event.getPlayer();
		boolean axe = player.getItemInHand().getType().name().contains("_AXE");
		if (axe) {
			Block block = event.getBlock();
			Skill woodcutting = SkillHandler.getSkill(player, Skills.WOODCUTTING);
			if (block.getType().equals(Material.LOG)) {
				switch (getWoodType(block)) {
				case OAK:
				case SPRUCE:
				case BIRCH:
					woodcutting.addExp(20);
					break;
				case JUNGLE:
					woodcutting.addExp(10);
					break;
				}	
			}
			else if (block.getType().equals(Material.LEAVES)) {
				ItemStack drop = getDrop(player, block);
				if (drop != null) {
					World world = block.getWorld();
					Location location = block.getLocation();
					world.dropItemNaturally(location, drop);
				}
			}
		}
	}

	private Wood getWoodType(Block block) {
		switch (block.getData()) {
		case 0:
		case 4:
		case 8:
		case 12:
		default:
			return Wood.OAK;
		case 1:
		case 5:
		case 9:
		case 13:
			return Wood.SPRUCE;
		case 2:
		case 6:
		case 10:
		case 14:
			return Wood.BIRCH;
		case 3:
		case 7:
		case 11:
		case 15:
			return Wood.JUNGLE;
		}
	}

	private ItemStack getDrop(Player player, Block block) {
		int level = SkillHandler.getSkill(player, Skills.WOODCUTTING).level();
		double chance = new Random().nextDouble();
		if (level >= 750) {
			switch (block.getData()) {
			case 0:
				if (chance <= 0.005) {
					return new ItemStack(Material.APPLE);
				}
			case 1:
			case 2:
				if (chance <= 0.05) {
					return new ItemStack(Material.SAPLING, 1, block.getData());
				}
			case 3:
				if (chance <= 0.025) {
					return new ItemStack(Material.SAPLING, 1, block.getData());
				}
			}
		}
		return null;
	}
}